package com.cuotigou.util;

import cn.jiguang.common.ClientConfig;
import cn.jiguang.common.ServiceHelper;
import cn.jiguang.common.connection.ApacheHttpClient;
import cn.jiguang.common.resp.ResponseWrapper;
import cn.jsms.api.SendSMSResult;
import cn.jsms.api.ValidSMSResult;
import cn.jsms.api.account.AccountBalanceResult;
import cn.jsms.api.account.AppBalanceResult;
import cn.jsms.api.common.model.BatchSMSPayload;
import cn.jsms.api.common.model.BatchSMSResult;
import cn.jsms.api.common.model.RecipientPayload;
import cn.jsms.api.schedule.model.ScheduleResult;
import cn.jsms.api.schedule.model.ScheduleSMSPayload;
import cn.jsms.api.template.SendTempSMSResult;
import cn.jsms.api.template.TempSMSResult;
import cn.jsms.api.template.TemplatePayload;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import cn.jiguang.common.resp.APIConnectionException;
import cn.jiguang.common.resp.APIRequestException;
import cn.jsms.api.common.SMSClient;
import cn.jsms.api.common.model.SMSPayload;

import java.util.ArrayList;
import java.util.List;


public class JSMSExample {

    protected static final Logger LOG = LoggerFactory.getLogger(JSMSExample.class);

    public static final String appkey = "90828c0f8de9da2ce4f1cd96";
    public static final String masterSecret = "110b3e731931fe8eece93edb";

    private static final String devKey = " 98ac69220cdb669218cb265d";
    private static final String devSecret = "f4331b3d0caeab47ea4211a4";

    public static void main(String[] args) {
        sendSMSCode("13758876025");
//        sendValidSMSCode("701651646304","455101");

    }

    //发送验证码
    public static String sendSMSCode(String phone) {
        SMSClient client = new SMSClient(masterSecret, appkey);
        SMSPayload payload = SMSPayload.newBuilder()
                .setMobileNumber(phone)
                .setTempId(1)
                .build();
        String result = null;
        try {
            SendSMSResult res = client.sendSMSCode(payload);
            LOG.info(res.toString());
            result = res.getMessageId();
            LOG.info(result);
            return result;//返回的是json{msg_id:"4346777"}
        } catch (APIConnectionException e) {
            LOG.error("Connection error. Should retry later. ", e);
        } catch (APIRequestException e) {
            LOG.error("Error response from JPush server. Should review and fix it. ", e);
            LOG.info("HTTP Status: " + e.getStatus());
            LOG.info("Error Message: " + e.getMessage());
        }
        return null;
    }

    //验证码校验
    public static Boolean sendValidSMSCode(String msg_id,String code) {
        SMSClient client = new SMSClient(masterSecret, appkey);
        Boolean valid = false;
        try {
            ValidSMSResult res = client.sendValidSMSCode(msg_id ,code);
            valid = res.getIsValid();
            LOG.info(res.toString());
        } catch (APIConnectionException e) {
            e.printStackTrace();
            LOG.error("Connection error. Should retry later. ", e);
        } catch (APIRequestException e) {
            e.printStackTrace();
            if (e.getErrorCode() == 50010) {
                // do something
            }
            LOG.info(e.getStatus() + " errorCode: " + e.getErrorCode() + " " + e.getErrorMessage());
            LOG.error("Error response from JPush server. Should review and fix it. ", e);
            LOG.info("HTTP Status: " + e.getStatus());
            LOG.info("Error Message: " + e.getMessage());
            return false;
        }
        return valid;
    }

}